<template>
  <div class="td-patient-me-mate-info">
    <mt-field label="配偶姓名" placeholder="请输入配偶姓名" v-model="params.MateName"></mt-field>
    <mt-field
      label="身份证号"
      placeholder="请输入身份证号"
      type="number"
      v-model="params.MateIDCard"
      @blur.native.capture="blurMateIDCard"
    ></mt-field>
    <mt-field
      label="生日"
      placeholder="请输入生日"
      v-if="loadData"
      v-model="params.MateBirthDay"
      @focus.native.capture="$refs['MateBirthday'].open()"
    ></mt-field>
    <div class="btnSubmit">
      <el-button
        type="primary"
        class="td-btn-patient-primary"
        style="width:80%;"
        round
        @click.native="btnSave();"
      >保存</el-button>
    </div>
    <mt-datetime-picker
      ref="MateBirthday"
      type="date"
      year-format="{value} 年"
      month-format="{value} 月"
      date-format="{value} 日"
      :startDate="new Date('1600-01-01')"
      v-if="loadData"
      v-model="date"
      @confirm="handleChange"
    ></mt-datetime-picker>
  </div>
</template>

<script>
import UserService from "../../../../_services/user.service";
export default {
  name: "TianDingPatientMeMateInfoComponent",
  data() {
    return {
      params: {
        UserID: 0,
        UserName: "",
        MobilePhone: "",
        Password: "",
        IDCard: "",
        Birthday: "",
        Email: "",
        MateName: "",
        MateBirthDay: "",
        MateIDCard: "",
        IconPath: "",
        RegisterTime: "",
        LoginTime: "",
        UserStatus: 0,
        OpenID: ""
      },
      date: new Date(),
      loadData: false
    };
  },
  mounted() {
    this.params = this.utils$.dataFormat(
      JSON.parse(this.local$.getItem("tdUser"))
    );

    if (this.params.MateBirthDay === "") {
      let date = this.utils$.getCurrentDate();
      this.params.MateBirthDay = `${date.year}-${date.month}-${date.day}`;
    } else {
      this.params.MateBirthDay = this.params.MateBirthDay.split("T")[0];
      this.date = new Date(this.params.MateBirthDay);
    }
    this.loadData = true;
  },
  methods: {
    blurMateIDCard() {
      if (!this.utils$.isEmpty(this.params.MateIDCard)) {
        return;
      }
      let year = this.params.MateIDCard.substring(6, 10);
      let month = this.params.MateIDCard.substring(10, 12);
      let day = this.params.MateIDCard.substring(12, 14);
      this.params.MateBirthDay = `${year}-${month}-${day}`;
    },
    handleChange(value) {
      console.log(value);
      let newDate = new Date(value).toLocaleDateString().replace(/\//g, "-");
      let year = newDate.split("-")[0];
      let month = parseInt(newDate.split("-")[1]);
      let day = parseInt(newDate.split("-")[2]);
      if (month < 10) {
        month = `0${month}`;
      }
      if (day < 10) {
        day = `0${day}`;
      }
      this.params.MateBirthDay = `${year}-${month}-${day}`;
    },
    btnSave() {
      if (!this.utils$.isEmpty(this.params.MateName)) {
        this.toast$("请输入配偶姓名");
        return;
      }

      if (!this.utils$.isEmpty(this.params.MateIDCard)) {
        this.toast$("请输入身份证号");
        return;
      }

      if (!this.utils$.isEmpty(this.params.MateBirthDay)) {
        this.toast$("请输入生日");
        return;
      }

      this.indicator$.open("请稍后...");
      this.params.Password = "";
      UserService.updateUserInfo(this.params).then(response => {
        setTimeout(() => {
          if (response && response.Data > 0) {
            this.toast$("保存成功");
          } else {
            this.toast$("保存失败");
          }
          this.indicator$.close();
        }, 1000);
        UserService.getUserInfoByID({ userID: this.params.UserID }).then(
          args => {
            if (args && args.Data) {
              args.Data.MateAge = this.utils$.getAge(args.Data.MateBirthDay);
              this.local$.removeItem("tdUser");
              this.local$.setItem("tdUser", JSON.stringify(args.Data));
            }
          }
        );
      });
    }
  }
};
</script>

